﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JChatCore.CounTry;
using JChatData.Helper;
using JChatSerVices.GetConnect;

namespace JChatAdmin.Controllers
{
    public class CountryController : Controller
    {
        //
        // GET: /Country/   

        GetFileConnect _get;
        DBConectString _db;
        JChatSerVices.CountryService.CountryService _country;
        JChatSerVices.CountryService.countryproperties _counTry;

        public CountryController()
        {
            _get = new GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
            _country = new JChatSerVices.CountryService.CountryService();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult List3() //done
        {
            var a = this._country.GetAllCountry();
            return View(a);
        }

        [HttpGet]
        public ActionResult List4(int _id) //test
        {
            var a = this._country.GetCountryByID(_id);
            return View(a);
        }

        [HttpPost]
        public ActionResult Add(CounTry _c)
        {
            if (ModelState.IsValid)
            {
                _country.AddCountry(_c);
                return RedirectToAction("List3");
            }
            return View();
        }

        public ActionResult AddCountry() //need upgrade
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddCountry(CounTry _c) //need upgrade
        {
            if (ModelState.IsValid)
            {
                _country.AddCountry(_c);
                return RedirectToAction("List3");
            }
            return View();
        }

        public ActionResult Delete(int id) //done
        {
            _country.DeleteCountry(id);
            return RedirectToAction("List3");
        }

        public ActionResult Update(int _id)
        {
            var a=  _country.GetCountryByID(_id);
            _counTry = new JChatSerVices.CountryService.countryproperties();
            foreach(JChatSerVices.CountryService.countryproperties p in a)
            {
                _counTry.NameCountry = p.NameCountry;
            }
            return View(_counTry);
        }

        [HttpPost]
        public ActionResult Update(int _id, CounTry _c)
        {
            if (ModelState.IsValid)
            {
                _country.UpdateCountry(_id, _c);
                return RedirectToAction("List3");
            }
            return View();
        }
    }
}
